package com.huawei.hgip.paintgame.adapter;


import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.huawei.hgip.paintgame.R;

/**
 * @author sunlian 20110520
 * @version 
 */
public class HistoryAdapter extends ArrayAdapter<HistoryInfo> {
    Activity activity;
    List<HistoryInfo> historyInfos;
    public HistoryAdapter(Activity activity, List<HistoryInfo> appLists) {
        super(activity, 0, appLists);
        this.activity = activity;
        historyInfos=appLists;

    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        LayoutInflater inflater = activity.getLayoutInflater();
        rowView = inflater.inflate(R.layout.log, null);
        HistoryInfo myApplicationInfo = getItem(position);
        ImageView sourceImage = (ImageView) rowView.findViewById(R.id.source);
        ImageView resultImage = (ImageView) rowView.findViewById(R.id.result);
        sourceImage.setImageBitmap(myApplicationInfo.source);
        resultImage.setImageBitmap(myApplicationInfo.result);
        ((TextView) rowView.findViewById(R.id.data))
                .setText(myApplicationInfo.data);
        
        
        //获取控件
        if (null!=sourceImage) {
        sourceImage.setOnClickListener(new OnClickListener() {
                 //@Override
                 public void onClick(View v) {
                    //Toast.makeText(activity,"sourceImage点击事件",Toast.LENGTH_SHORT).show();
             		
                    String sourceUri = historyInfos.get(position).sourceUri;
            		System.out.println("Current source Uri=" + sourceUri);
            		// 通过intent传递信息
            		Intent intent = new Intent();
            		intent.putExtra("sourceUri", sourceUri);
            		Bitmap sourceBitmap = historyInfos.get(position).source;
            		//Drawable source = new BitmapDrawable(sourceBitmap); 
            		Dialog dialog = new Dialog(activity); 
            		dialog.setContentView(R.layout.history_dialog);
            		dialog.setTitle("涂鸦：");
            		ImageView image = (ImageView) dialog.findViewById(R.id.big_pic);
            		image.setImageBitmap(sourceBitmap);
            		TextView picUri = (TextView) dialog.findViewById(R.id.pic_uri);
            		picUri.setText(sourceUri);
            		dialog.show();

                 }
             });
        }
        //获取控件
        if (null!=resultImage) {
        	resultImage.setOnClickListener(new OnClickListener() {
                 //@Override
                 public void onClick(View v) {
                    //Toast.makeText(activity,"resultImage点击事件",Toast.LENGTH_SHORT).show();
             
                    String resultUri = historyInfos.get(position).resultUri;
            		System.out.println("Current result Uri=" + resultUri);
            		// 通过intent传递信息
            		Intent intent = new Intent();
            		intent.putExtra("resultUri", resultUri);
            		Bitmap resultBitmap = historyInfos.get(position).result;
            		//Drawable result = new BitmapDrawable(resultBitmap); 
            		//title不能为空，先设置为 空格
            		Dialog dialog = new Dialog(activity); 
            		dialog.setContentView(R.layout.history_dialog);
            		dialog.setTitle("结果图片：");
            		ImageView image = (ImageView) dialog.findViewById(R.id.big_pic);
            		image.setImageBitmap(resultBitmap);
            		TextView picUri = (TextView) dialog.findViewById(R.id.pic_uri);
            		picUri.setText(resultUri);
            		dialog.show();
                 }
             });
        }

        
        return rowView;
    }
}